package com.example.myapplication.lawyer;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.myapplication.Config.OKHttpTool;
import com.example.myapplication.Config.TokenBean;
import com.example.myapplication.R;
import com.example.myapplication.ShuJu.LawyerdetailsBean;
import com.example.myapplication.ShuJu.LegalexpertiseBean;
import com.example.myapplication.ShuJu.SubmitlegaladviceBean;
import com.example.myapplication.apter.LegalexpertiseApter;
import com.google.android.material.tabs.TabLayout;

import org.json.JSONException;
import org.json.JSONObject;

public class LawyerconsultationActivity extends AppCompatActivity {

    private String lawyerlistpageActivityID;
    private ImageView img_topchoicelawyy;
    private TextView name_topchoicelawyy;
    private TextView text_topchoicelawyy;
    private TextView year_topchoicelawyy;
    private TextView num_topchoicelawyy;
    private TextView text_select;
    private ImageView but_lawyerconsultationt;
    private LinearLayout ly_select;
    private LinearLayout li_submit;
    private EditText ed_problem;
    private EditText ed_telephone;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lawyerconsultation);
        ivitview();
        //返回
        but_bloak();
        //显示律师线详情
        lawyerdetails();
        //点击选择弹出律师专长
        lawyerspecialty();

    }

    private void lawyerspecialty() {
        ly_select.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获取律师专长
                Legalexpertise();
            }
        });
    }

    private void but_bloak() {
        but_lawyerconsultationt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LawyerconsultationActivity.this.finish();
            }
        });
    }

    private void Legalexpertise() {
        OKHttpTool.doNetRequest("prod-api/api/lawyer-consultation/legal-expertise/list",
                OKHttpTool.GET,
                LegalexpertiseBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        LegalexpertiseBean legalexpertiseBean = (LegalexpertiseBean) obj;
                        if (legalexpertiseBean.getCode() == 200) {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    //弹出律师专长
                                    screen(legalexpertiseBean);
                                }
                            });
                        }
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                });
    }


    //弹出律师专长
    private void screen(LegalexpertiseBean legalexpertiseBean) {
        ly_select.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(LawyerconsultationActivity.this);
                View inflate = getLayoutInflater().inflate(R.layout.item_sort_lawyerlist, null);
                RecyclerView re_itemsort = inflate.findViewById(R.id.re_itemsort);
                GridLayoutManager gridLayoutManager = new GridLayoutManager(LawyerconsultationActivity.this, 4);
                LegalexpertiseApter legalexpertiseApter = new LegalexpertiseApter(LawyerconsultationActivity.this, legalexpertiseBean.getRows());
                re_itemsort.setLayoutManager(gridLayoutManager);
                re_itemsort.setAdapter(legalexpertiseApter);
                builder.setView(inflate);

                builder.setPositiveButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                builder.setNegativeButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        text_select.setText(legalexpertiseApter.getLawyerName());
                        //法律1专长ID
                        Integer lawyerID = legalexpertiseApter.getLawyerID();
                        //添加法律咨询
                        addlegaladvice(lawyerID);
                    }
                });
                builder.create().show();
            }
        });
    }

    //添加法律咨询
    private void addlegaladvice(Integer lawyerID) {
        li_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String ed_problemText = ed_problem.getText().toString();
                String ed_telephoneText = ed_telephone.getText().toString();
                JSONObject jsonObject = new JSONObject();
                try {
                    jsonObject.put("lawyerId",lawyerlistpageActivityID);
                    jsonObject.put("legalExpertiseId",lawyerID);
                    jsonObject.put("content",ed_problemText);
                    jsonObject.put("phone",ed_telephoneText);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                String JSON = jsonObject.toString();
                //提交法律咨询
                submitlegaladvice(JSON);
            }
        });
    }

    private void submitlegaladvice(String JSON) {
        OKHttpTool.doNetRequest("prod-api/api/lawyer-consultation/legal-advice",
                OKHttpTool.POST,
                SubmitlegaladviceBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                SubmitlegaladviceBean submitlegaladviceBean = (SubmitlegaladviceBean)obj;
                                if(submitlegaladviceBean.getCode()==200){
                                    Toast.makeText(LawyerconsultationActivity.this,"提交诉求成功",Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                },JSON);
    }

    private void lawyerdetails() {
        OKHttpTool.doNetRequest("prod-api/api/lawyer-consultation/lawyer/" + lawyerlistpageActivityID,
                OKHttpTool.GET,
                LawyerdetailsBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        LawyerdetailsBean lawyerdetailsBean = (LawyerdetailsBean) obj;
                        if (lawyerdetailsBean.getCode() == 200) {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    LawyerdetailsBean.DataBean  data = lawyerdetailsBean.getData();
                                    Glide.with(LawyerconsultationActivity.this).load(TokenBean.IP + data.getAvatarUrl()).into(img_topchoicelawyy);
                                    name_topchoicelawyy.setText(data.getName());
                                    text_topchoicelawyy.setText(data.getLegalExpertiseName());
                                    year_topchoicelawyy.setText(data.getWorkStartAt());
                                    num_topchoicelawyy.setText(String.valueOf(data.getServiceTimes()));
                                    name_topchoicelawyy.setText(data.getName());
                                }
                            });
                        }
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                });
    }

    private void ivitview() {
        Intent intent = getIntent();
        //律师ID
        lawyerlistpageActivityID = intent.getStringExtra("LawyerconsultationActivityID");

        img_topchoicelawyy = findViewById(R.id.img_topchoicelawyy);
        but_lawyerconsultationt = findViewById(R.id.but_lawyerconsultationt);
        name_topchoicelawyy = findViewById(R.id.Name_topchoicelawyy);
        text_topchoicelawyy = findViewById(R.id.text_topchoicelawyy);
        year_topchoicelawyy = findViewById(R.id.year_topchoicelawyy);
        num_topchoicelawyy = findViewById(R.id.num_topchoicelawyy);

        ly_select = findViewById(R.id.ly_select);
        li_submit = findViewById(R.id.li_submit);
        ed_problem = findViewById(R.id.ed_problem);
        ed_telephone = findViewById(R.id.ed_telephone);
        text_select = findViewById(R.id.text_select);

    }
}